package com.flyoil.petromp.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aigestudio.wheelpicker.WheelPicker;
import com.aigestudio.wheelpicker.widgets.WheelDayPicker;
import com.aigestudio.wheelpicker.widgets.WheelMonthPicker;
import com.aigestudio.wheelpicker.widgets.WheelYearPicker;
import com.flyoil.petromp.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by songzhiyin on 2017/11/1.
 * 自定义日期选择器
 */

public class MyDatePickerView extends LinearLayout {
    private View view;
    private TextView tv_select_name, tv_confirm, tv_cancle;
    private WheelYearPicker wheel_year;
    private WheelMonthPicker wheel_month;
    private WheelDayPicker wheel_day;
    private int year, month, day;

    public MyDatePickerView(Context context) {
        super(context);
        initview(context);
    }

    public MyDatePickerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initview(context);
    }

    public MyDatePickerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initview(context);
    }

    private void initview(Context context) {
        view = LayoutInflater.from(context).inflate(R.layout.view_mydate_layout, this);
        tv_select_name = view.findViewById(R.id.tv_date_view_select_time);
        tv_cancle = view.findViewById(R.id.tv_date_view_select_cancel);
        tv_confirm = view.findViewById(R.id.tv_date_view_select_confirm);
        wheel_year = view.findViewById(R.id.whlv_date_view_select_year);
        wheel_month = view.findViewById(R.id.whlv_date_view_select_month);
        wheel_day = view.findViewById(R.id.whlv_date_view_select_day);
        initdata();
        initOnLitener();
    }

    /**
     * 初始化数据
     */
    private void initdata() {
        wheel_year.setYearFrame(2010, 2030);
        wheel_year.setSelectedYear(2017);
        List<String> month_data = new ArrayList<>();
//        for (int i = 0; i < 12; i++) {
//            month_data.add((i + 1) + "月");
//        }
//        wheel_month.setData(month_data);
        wheel_month.setSelectedMonth(6);
        wheel_day.setYearAndMonth(wheel_year.getSelectedYear(), wheel_month.getSelectedMonth());
        wheel_day.setSelectedDay(10);
        setTv_select_name();
    }

    private void initOnLitener() {
        wheel_year.setOnItemSelectedListener(new WheelPicker.OnItemSelectedListener() {
            @Override
            public void onItemSelected(WheelPicker picker, Object data, int position) {
                setTv_select_name();
            }
        });
        wheel_month.setOnItemSelectedListener(new WheelPicker.OnItemSelectedListener() {
            @Override
            public void onItemSelected(WheelPicker picker, Object data, int position) {
                wheel_day.setYearAndMonth(wheel_year.getCurrentYear(), wheel_month.getCurrentMonth());
                setTv_select_name();
            }
        });
        wheel_day.setOnItemSelectedListener(new WheelPicker.OnItemSelectedListener() {
            @Override
            public void onItemSelected(WheelPicker picker, Object data, int position) {
                setTv_select_name();
            }
        });

    }

    /**
     * 设置取消按钮的点击事件的监听
     *
     * @param onClickListener
     */
    public void setCancleOnClickListener(View.OnClickListener onClickListener) {
        if (onClickListener == null) {
            return;
        }
        tv_cancle.setOnClickListener(onClickListener);
    }

    /**
     * 设置确定按钮的点击事件的监听事件
     *
     * @param onClickListener
     */
    public void setConfirmOnClickListener(View.OnClickListener onClickListener) {
        if (onClickListener == null) {
            return;
        }
        tv_confirm.setOnClickListener(onClickListener);
    }

    public int getYear() {
        return year;
    }

    public int getMonth() {
        return month;
    }

    public int getDay() {
        return day;
    }

    /**
     * 设置选中的年月日
     */
    private void setTv_select_name() {
        year = wheel_year.getCurrentYear();
        month = wheel_month.getCurrentMonth();
        day = wheel_day.getCurrentDay();
        tv_select_name.setText(year + "年 " + month + "月 " + day + "日");
    }
}
